#include "PauseState.h"

#include <iostream>
#include "common_include.h"
#include "OptionState.h"
using std::cout;


PauseState::PauseState(void) : BaseState()
{
   m_iStateType = STATE_PAUSE;
   m_bUpdateScreen = true;
}


PauseState::~PauseState(void)
{
}

void PauseState::OnEnter()
{
   // Don't forget to call the base state's function so we get base variables set correctly
   BaseState::OnEnter();

   m_eCurMenuSelect = PAUSE_RESUME_GAME;
   m_bUpdateScreen = true;
}

void PauseState::Update(float fTime)
{
   // TODO: add any updates required here
}

void PauseState::Render()
{
   // Don't draw if we don't have to.
   if (!m_bUpdateScreen)
      return;

   // Render main menu options
   static char * sMenuText[PAUSE_MAX_SELECT] = {"RESUME GAME", "SAVE GAME", "OPTIONS", "QUIT TO MAIN MENU", "EXIT GAME"};
   char sTextBuf[64];

   // Clear the old screen
   system("cls");

   // TODO: render Title
   cout << "\n\n\nThis Is A Game\n\n\n\n";
   for (unsigned int iSelect = 0; iSelect < PAUSE_MAX_SELECT; ++iSelect)
   {
      char sTemp[4] = "   ";
      if (iSelect == m_eCurMenuSelect)
         strcpy_s(sTemp, sizeof(sTemp), "-> ");

      sprintf_s(sTextBuf, sizeof(sTextBuf), "%s%s\n\n", sTemp, sMenuText[iSelect]);
      // Draw to the console
      cout << sTextBuf;
   }

   m_bUpdateScreen = false;
}

void PauseState::GetInput()
{
   if (GetAsyncKeyState(VK_RETURN) & 0x81)
   {
      // Switch to current selected state (new game, load game, options, or exit)
      // TODO: fill in remaining entries, too lazy to fill in at the moment
      switch (m_eCurMenuSelect)
      {
      case PAUSE_SAVE_GAME:
         GameManager::GetInstance()->SetGameState(STATE_MAINGAME);
         break;
      case PAUSE_OPTIONS_SELECT:
         // Switch to the Options page
         ((OptionState*)GameManager::GetInstance()->GetGameStatePtr(STATE_OPTIONS))->SetPreviousState(m_iStateType);
         GameManager::GetInstance()->SetGameState(STATE_OPTIONS);
         break;
      case PAUSE_EXIT_GAME: // Shutdown the game when exit is selected
         GameManager::GetInstance()->SetGameState(STATE_SHUTDOWN);
         break;
      default:
         // Default to shutdown if we aren't handling a selection
         GameManager::GetInstance()->SetGameState(STATE_SHUTDOWN);
         break;
      }
   }

   if (GetAsyncKeyState(VK_UP) & 0x81)
   {
      m_bUpdateScreen = true;
      m_eCurMenuSelect = (PauseMenuSelections)(m_eCurMenuSelect - 1);
      // Loop back around to the last selection
      if (m_eCurMenuSelect < 0)
         m_eCurMenuSelect = (PauseMenuSelections)(PAUSE_MAX_SELECT - 1);
   }
   if (GetAsyncKeyState(VK_DOWN) & 0x81)
   {
      m_bUpdateScreen = true;
      m_eCurMenuSelect = (PauseMenuSelections)(m_eCurMenuSelect + 1);
      // Loop back around to the first selection
      if (m_eCurMenuSelect >= PAUSE_MAX_SELECT)
         m_eCurMenuSelect = PAUSE_RESUME_GAME;// TODO: this may not be guaranteed to be index 0 (find a better way to loop back around to first index)
   }
}
